package com.xhwl.centiotdata.repair.imp;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.repair.IRepairTask;
import com.xhwl.centiotdata.service.integrateconfig.DeviceProjSupplierCategoryDTO;
import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfig;
import com.xhwl.centiotdata.suppliersystem.bailian.SaleBaiLianHandler;
import com.xhwl.common.dto.cent.iotdata.repair.RepairActionDTO;
import com.xhwl.common.enums.iot.RepairTypeEnum;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.utils.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Copyright (C),1998-2022, 兴海物联
 * FileName: PedestrianSyncTask
 * Author: XuChang
 * Date: 2022/8/9 19:07
 * Description: 销售记录同步任务
 * History:
 */
@Component
public class SaleSyncTask implements IRepairTask<DeviceProjSupplierCategory> {

    SaleBaiLianHandler saleBaiLianHandler;

    @Autowired
    public void setSaleBaiLianHandler(SaleBaiLianHandler saleBaiLianHandler) {
        this.saleBaiLianHandler = saleBaiLianHandler;
    }

    @Override
    public int getType() {
        return RepairTypeEnum.SALE.getType();
    }

    @Override
    public DeviceProjSupplierCategory getConfig(RepairActionDTO repairActionDTO) {
        Optional<DeviceProjSupplierCategory> optional = IntegrationConfig.integrationConfigList.stream()
                .filter(integrationConfig -> integrationConfig.getProductId() != null && integrationConfig.getProductId().intValue() == 9 && integrationConfig.getProdCategoryId() == null
                        && repairActionDTO.getProjectId().equals(integrationConfig.getProjectId()))
                .findFirst();
        return optional.isPresent() ? optional.get() : null;
    }

    @Override
    public String repairAction(Date date, DeviceProjSupplierCategory deviceProjSupplierCategory) {
        JSONObject properties = deviceProjSupplierCategory.getProperties();
        Integer projectId = deviceProjSupplierCategory.getProjectId();
        String ipAndPort = properties.getString("ipAndPort");
        String mockModeKey = "isMockMode";
        Boolean isMockMode = properties.containsKey(mockModeKey) ? properties.getBoolean(mockModeKey) : false;
        String dateStr = CalendarUtil.getDateStr(CalendarUtil.DATE_FORMAT[1], date);
        return saleBaiLianHandler.pullSalesReturnError(projectId, ipAndPort, dateStr, isMockMode);
    }

}
